package cn.ekuma.data.ui.android;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import cn.ekuma.data.dao.bean.I_ViewBean;

public class BaseBeanListSelecter<T extends I_ViewBean> extends LinearLayout implements OnItemLongClickListener{
	
	ListView listview; 
	
	OnBaseBeanSelectedListener l;
	
	BaseAdapter adapter;

    public static final int INVALID_POSITION = -1;

    private int  selectItem=INVALID_POSITION;


    public BaseBeanListSelecter(Context context) {
		super(context);
		 listview=new ListView(context);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
		 this.addView(listview,lp);
		 listview.setOnItemLongClickListener(this); 
	}
	
	public BaseBeanListSelecter(Context context, AttributeSet attrs) {
		super(context, attrs);
		listview=new ListView(context); 
		this.addView(listview);
		
    }  
 
	
    public void setAbstractBaseBeanAdapter(final BaseAdapter adapter) {
    	this.adapter=adapter;
    	listview.setAdapter(adapter);
    	listview.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
                setSelectItem(position);
				fireBaseBeanSelected((T) adapter.getItem(position));
			}
    	});
    }
    
    public void fireBaseBeanSelected(T obj){
    	if(l!=null)
			l.baseBeanSelected(obj,selectItem);
    }
    
    
    public void setOnBaseBeanSelectedListener(OnBaseBeanSelectedListener l){
    	this.l=l;
    }
    
    public void removeOnBaseBeanSelectedListener(){
    	this.l=null;
    }

	public ListView getListview() {
		return listview;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setSelectionAtPosition(int i) {
		if(i>=adapter.getCount())
			i=adapter.getCount()-1;
		setSelectItem(i);
		if(getSelectItem()!= AbstractViewBeanListAdapter.INVALID_POSITION)
		      listview.smoothScrollToPosition(getSelectItem());
	}


    public int getSelectItem() {
        return selectItem;
    }

    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }

    public void clearSelect(){
        selectItem=INVALID_POSITION;
    }

    public T getSelectBean() {
        if(selectItem!=INVALID_POSITION)
            return (T)adapter.getItem(selectItem);
        return null;
    }


    public void moveUp(){

        if(selectItem!= AbstractViewBeanListAdapter.INVALID_POSITION&&selectItem>0){
            --selectItem;
        }
        if(getSelectItem()!= AbstractViewBeanListAdapter.INVALID_POSITION)
            listview.smoothScrollToPosition(getSelectItem());

    };

    public void moveDown(){

        if(selectItem<adapter.getCount()-1){
            ++selectItem;

        }
        if(getSelectItem()!= AbstractViewBeanListAdapter.INVALID_POSITION)
            listview.smoothScrollToPosition(getSelectItem());
    }

    public void clear() {
        clearSelect();
    }


    public void remove(int position) {

        //adapter.remove(adapter.getItem(position));
    }


}
